home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / dns / resolv+2.1.1 / res_init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-28  |  5.5 KB  |  183 lines

  1. /*-
  2.  * Copyright (c) 1985, 1989 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)res_init.c    6.14 (Berkeley) 6/27/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <stdio.h>
  28. #include <arpa/nameser.h>
  29. #include <resolv.h>
  30.  
  31. /*
  32.  * Resolver state default settings
  33.  */
  34.  
  35. #ifdef SHLIB
  36. extern struct state _res;
  37. #else
  38. struct state _res = {
  39.     RES_TIMEOUT,                   /* retransmition time interval */
  40.     4,                             /* number of times to retransmit */
  41.     RES_DEFAULT,            /* options flags */
  42.     1,                             /* number of name servers */
  43. };
  44. #endif
  45.  
  46. /*
  47.  * Set up default settings.  If the configuration file exist, the values
  48.  * there will have precedence.  Otherwise, the server address is set to
  49.  * INADDR_ANY and the default domain name comes from the gethostname().
  50.  *
  51.  * The configuration file should only be used if you want to redefine your
  52.  * domain or run without a server on your machine.
  53.  *
  54.  * Return 0 if completes successfully, -1 on error
  55.  */
  56. res_init()
  57. {
  58.     register FILE *fp;
  59.     register char *cp, **pp;
  60.     register int n;
  61.     char buf[BUFSIZ];
  62.     extern u_long inet_addr();
  63.     extern char *index();
  64.     extern char *strcpy(), *strncpy();
  65.     extern char *getenv();
  66.     int nserv = 0;    /* number of nameserver records read from file */
  67.     int haveenv = 0;
  68.     int havesearch = 0;
  69.  
  70.     _res.nsaddr.sin_addr.s_addr = INADDR_ANY;
  71.     _res.nsaddr.sin_family = AF_INET;
  72.     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  73.     _res.nscount = 1;
  74.  
  75.     /* Allow user to override the local domain definition */
  76.     if ((cp = getenv("LOCALDOMAIN")) != NULL) {
  77.         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  78.         haveenv++;
  79.     }
  80.  
  81.     if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) {
  82.         /* read the config file */
  83.         while (fgets(buf, sizeof(buf), fp) != NULL) {
  84.         /* read default domain name */
  85.         if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
  86.             if (haveenv)    /* skip if have from environ */
  87.                 continue;
  88.             cp = buf + sizeof("domain") - 1;
  89.             while (*cp == ' ' || *cp == '\t' || *cp == '.')
  90.                 cp++;
  91.             if ((*cp == '\0') || (*cp == '\n'))
  92.                 continue;
  93.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  94.             if ((cp = index(_res.defdname, '\n')) != NULL)
  95.                 *cp = '\0';
  96.             havesearch = 0;
  97.             continue;
  98.         }
  99.         /* set search list */
  100.         if (!strncmp(buf, "search", sizeof("search") - 1)) {
  101.             if (haveenv)    /* skip if have from environ */
  102.                 continue;
  103.             cp = buf + sizeof("search") - 1;
  104.             while (*cp == ' ' || *cp == '\t')
  105.                 cp++;
  106.             if ((*cp == '\0') || (*cp == '\n'))
  107.                 continue;
  108.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  109.             if ((cp = index(_res.defdname, '\n')) != NULL)
  110.                 *cp = '\0';
  111.             /*
  112.              * Set search list to be blank-separated strings
  113.              * on rest of line.
  114.              */
  115.             cp = _res.defdname;
  116.             pp = _res.dnsrch;
  117.             *pp++ = cp;
  118.             for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
  119.                 if (*cp == ' ' || *cp == '\t') {
  120.                     *cp = 0;
  121.                     n = 1;
  122.                 } else if (n) {
  123.                     *pp++ = cp;
  124.                     n = 0;
  125.                 }
  126.             }
  127.             /* null terminate last domain if there are excess */
  128.             while (*cp != '\0' && *cp != ' ' && *cp != '\t')
  129.                 cp++;
  130.             *cp = '\0';
  131.             *pp++ = 0;
  132.             havesearch = 1;
  133.             continue;
  134.         }
  135.         /* read nameservers to query */
  136.         if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
  137.            nserv < MAXNS) {
  138.             cp = buf + sizeof("nameserver") - 1;
  139.             while (*cp == ' ' || *cp == '\t')
  140.                 cp++;
  141.             if ((*cp == '\0') || (*cp == '\n'))
  142.                 continue;
  143.             if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
  144.             inet_addr(cp)) == (unsigned)-1) {
  145.                 _res.nsaddr_list[nserv].sin_addr.s_addr
  146.                 = INADDR_ANY;
  147.                 continue;
  148.             }
  149.             _res.nsaddr_list[nserv].sin_family = AF_INET;
  150.             _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
  151.             nserv++;
  152.             continue;
  153.         }
  154.         }
  155.         if (nserv > 1) 
  156.         _res.nscount = nserv;
  157.         (void) fclose(fp);
  158.     }
  159.     if (_res.defdname[0] == 0) {
  160.         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
  161.            (cp = index(buf, '.')))
  162.             (void)strcpy(_res.defdname, cp + 1);
  163.     }
  164.  
  165.     /* find components of local domain that might be searched */
  166.     if (havesearch == 0) {
  167.         pp = _res.dnsrch;
  168.         *pp++ = _res.defdname;
  169.         for (cp = _res.defdname, n = 0; *cp; cp++)
  170.             if (*cp == '.')
  171.                 n++;
  172.         cp = _res.defdname;
  173.         for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH;
  174.             n--) {
  175.             cp = index(cp, '.');
  176.             *pp++ = ++cp;
  177.         }
  178.         *pp++ = 0;
  179.     }
  180.     _res.options |= RES_INIT;
  181.     return (0);
  182. }
  183.